/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.dto.mapping;

import cz.muni.fi.pa165.creatures.dto.WeaponDTO;
import cz.muni.fi.pa165.creatures.dto.mapping.api.MappingDTO;
import cz.muni.fi.pa165.creatures.model.Weapon;

/**
 * This class represents the mapping between
 * {@link cz.muni.fi.pa165.creatures.model.Weapon} and
 * {@link cz.muni.fi.pa165.creatures.dto.WeaponDTO} and vice versa.
 *
 * @author smikloso
 */
public class WeaponMapping implements MappingDTO<WeaponDTO, Weapon> {

    @Override
    public WeaponDTO entityToDTO(Weapon weapon) {
        if (weapon == null) {
            return null;
        }

        WeaponDTO dto = new WeaponDTO();

        if (weapon.getId() != null) {
            dto.setId(weapon.getId().toString());
        }

        if (weapon.getRange() != null) {
            dto.setRange(weapon.getRange());
        }

        if (weapon.getAmmunition() != null) {
            dto.setAmmunition(weapon.getAmmunition());
        }

        if (weapon.getName() != null) {
            dto.setName(weapon.getName());
        }

        return dto;
    }

    @Override
    public Weapon DTOtoEntity(WeaponDTO dto) {
        if (dto == null) {
            return null;
        }

        Weapon weapon = new Weapon();
        if (dto.getId() != null) {
            weapon.setId(Long.parseLong(dto.getId()));
        }

        if (dto.getName() != null) {
            weapon.setName(dto.getName());
        }

        if (dto.getRange() != null) {
            weapon.setRange(dto.getRange());
        }

        if (dto.getAmmunition() != null) {
            weapon.setAmmunition(dto.getAmmunition());
        }

        return weapon;
    }
}
